<?php

function clean_html_output($buffer) {
    $buffer = preg_replace('!/\*[^*]*\*+([^/][^*]*\*+)*/!', '', $buffer); // remove comments
    $buffer = str_replace(array("\r\n", "\r", "\n", "\t"), '', $buffer); // remove tabs, spaces, newlines, etc.
    $buffer = str_replace('  ', ' ', $buffer);
    return $buffer;
}

error_reporting(1);
ob_implicit_flush(0);
session_start();

defined('BASE_PATH') || define('BASE_PATH', realpath(dirname(__FILE__)));
include_once ("config/config.php");
include_once ("config/common.php");
ob_start("replaceTitle");
if (isset($_GET ['path']) && $_GET ['path'] != '') {
    $page = filter($_GET ['path']);
} else {
    $page = 'home';
}

$params = explode('/', $page);


//$params[0] is difine for page
//$params[1] is difine for module
/* * **************************************BEGIN: ADMIN*********************************************** */
if ($params[0] == $config ['admin_section'] || $params[0] == 'favicon.ico') {//admin
// remember url
    
    if (isset($_GET ['path']) AND !strpos($_GET['path'], 'login') AND !strpos($_GET['path'], 'logout')) {
        $_SESSION['path'] = $page;
    }
    
    if (count($params) >= 2 AND $params[1] != '' AND $params[1] != 'favicon.ico') {
        $adminModule = $params[1];
        if (@$params[2] != '' && intval($params[2]) > 0) {
            $_SESSION['articlePageEdit'] = $config ['page'] = $params[2];
        } else {
            $config ['page'] = 1;
        }
    } else {
        $adminModule = 'article';
    }
    $_SESSION['adminActiveMenu'] = $adminModule;
    if (file_exists($config ['admin_function_path'] . "/$adminModule.php")) {
        include_once $config ['admin_function_path'] . "/$adminModule.php";
    }
    
    if (file_exists($config ['admin_document_root'] . "/modules/$adminModule.php")) {
        include_once $config ['admin_document_root'] . "/modules/$adminModule.php";
    } else {
        header("Location: " . $config['domain']);
    }
    
} else {//client
    if ($params[0] != 'rss') {
        ob_start("clean_html_output");
    }
    /*     * **************************************BEGIN: CLIENT*********************************************** */
    $_SESSION['clientActiveMenu'] = $params[0];
    if (count(explode('-', $params[0])) == 3) {
        $arr_link_list_archive = explode('-', $params[0]);
    }
    /*     * ****************************PAGE: DETAIL********************************* */
    if (strpos($params[0], '.html')) {//detai page
        $_SESSION['clientActiveMenu'] = 'detail';
        $toGetArticleLink = explode('.', $params[0]);
        $config ['link'] = $toGetArticleLink[0]; //article link
        if (file_exists($config ['client_model_path'] . "/detail.php")) {
            include_once $config ['client_model_path'] . "/detail.php";
        }
        if (file_exists($config ['client_document_root'] . "/modules/detail.php")) {
            include_once $config ['client_document_root'] . "/modules/detail.php";
        } else {
            header("Location: " . $config['domain']);
        }
        /*         * ****************************PAGE: CATE********************************* */
    } else if ($params[0] == 'cata') {
        $_SESSION['pathWay'] = $config ['link'] = $params[1];

        if (intval($params[3]) > 0) {
            $config ['page'] = $params[3];
        }


        /*         * ****************************PAGE: CATE CAC CAP***************** */
        if (file_exists($config ['client_model_path'] . "/cate.php")) {
            include_once $config ['client_model_path'] . "/cate.php";
        }

        if (file_exists($config ['client_document_root'] . "/modules/cate.php")) {
            include_once $config ['client_document_root'] . "/modules/cate.php";
        } else {
            header("Location: " . $config['domain']);
        }

        /*         * ****************************PAGE: DU AN***************** */
    } else if ($params[0] == 'rss') {
        $config ['link'] = $params[1];
        if (intval($params[3]) > 0) {
            $config ['page'] = $params[3];
        }
        if (file_exists($config ['client_model_path'] . "/rss.php")) {
            include_once $config ['client_model_path'] . "/rss.php";
        }
        if (file_exists($config ['client_document_root'] . "/modules/rss.php")) {
            if ($config ['link'] == '') {
                include_once $config ['client_document_root'] . "/modules/rssListUrl.php";
            } else {
                include_once $config ['client_document_root'] . "/modules/rss.php";
            }
        } else {
            header("Location: " . $config['domain']);
        }
        /*         * ****************************PAGE: SEARCH***************** */
    } else if ($params[0] == 'search') {
        if ($params[1] == '' || $params[1] == ' ' || $params[1] == '"' || $params[1] == '\'' || $params[1] == '*') {
            header("Location: " . $config['domain']);
        } else {
            $config['keyword'] = $params[1];
        }
        if (intval($params[3]) > 0) {
            $config ['page'] = $params[3];
        } else {
            $config ['page'] = 1;
        }
        if (file_exists($config ['client_model_path'] . "/search.php")) {
            include_once $config ['client_model_path'] . "/search.php";
        }
        if (file_exists($config ['client_document_root'] . "/modules/search.php")) {
            include_once $config ['client_document_root'] . "/modules/search.php";
        } else {
            header("Location: " . $config['domain']);
        }
        /**         * ***************************PAGE: TAG***************** */
    } else if ($params[0] == 'tag') {
        $_SESSION['pathWay'] = $config ['link'] = $params[1];
        if (intval($params[3]) > 0) {
            $config ['page'] = $params[3];
        }
        if (file_exists($config ['client_model_path'] . "/tag.php")) {
            include_once $config ['client_model_path'] . "/tag.php";
        }
        if (file_exists($config ['client_model_path'] . "/cate.php")) {
            include_once $config ['client_model_path'] . "/cate.php";
        }
        if (file_exists($config ['client_document_root'] . "/modules/tag.php")) {
            include_once $config ['client_document_root'] . "/modules/tag.php";
        } else {
            header("Location: " . $config['domain']);
        }
        /*         * ****************************PAGE: NEWS LETTER***************** */
    } else {
        $arr_home_paging = explode('_', $params[0]);
        $config ['page'] = intval($arr_home_paging[1]);
        if($config ['page']<=0){
            $config ['page'] = 1;
        }
        
        if (file_exists($config ['client_model_path'] . "/home.php")) {
            include_once $config ['client_model_path'] . "/home.php";
            $home = new home();
        }
        if (file_exists($config ['client_document_root'] . "/modules/home.php")) {
            include_once $config ['client_document_root'] . "/modules/home.php";
        } else {
            header("Location: " . $config['domain']);
        }
    }
}
ob_end_flush();
?>
